<?php

namespace app\order\logic;

use app\data\util\HttpUtil;
use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;

class OrderCheckSkuLogic
{
    protected $model = null;
    public function __construct()
    {
        $this->model = new \app\order\model\OrderCheckSku();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $date = $params['date'] ?? [];
        $type = $params['type'] ?? [];
        $spu = $params['spu'] ?? [];
        $sku = $params['sku'] ?? [];
        $limit = $params['limit'] ?? 10;
        $where = [];
        
        if(!empty($type)){
            $where['type'] = $type;
        }
        if(!empty($date)){
            $where['date'] = $date;
        }

        if($spu){
            $where[] = ['spu','=',$spu];
        }

        if($sku){
            $where[] = ['sku','=',$sku];
        }

        $list = Db::name('ordercheck_skucost')
            ->where($where)
            ->order("timedate desc,id asc")
            ->paginate($limit);

        $lists = $list->items();
                
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $lists = [];
        $header = [];
        // $header[] = ['广告账户', 'advertiser_id'];
        $fileName = "export_Advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    public function find($id)
    {
        if (!$id) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $row = $this->model->find($id);
        
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }

        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;
        
        try {
            
            foreach ($data as $value) {  
                if(!empty($value["material"])){
                    $value["material_total"] = number_format((float)$value["material"] * $value["count"], 3, '.', '');
                }
                $updateData = [
                    'material' => $value["material"] ?? 0,
                    'count' => $value["count"] ?? 0,
                    'material_total' => $value["material_total"] ?? 0,
                ];
                table('ordercheck_skucost')
                    ->where('id', $value['id'])
                    ->update($updateData);
            }

            $result = true;
            
            if ($result) {
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));
    }
    
    
}
